import { POWERS } from "./Consts";
import { Router } from "./Router";
import { RouterBase } from "./RouterBase";

export class ManagementRouter extends RouterBase {
  /**
   * 通过路由名称、图标及标题构造一个管理类页面路由
   * @param {string} name 路由名称
   * @param {string} icon 路由图标
   * @param {string} title 路由标题
   */
  constructor(name, icon, title) {
    super(name, icon, title);
    this.path('');
    this.powers([
      POWERS.BROWSER,
      POWERS.CREATE,
      POWERS.UPDATE,
      POWERS.REMOVE
    ]);
  }
  /**
   * 设置查询界面组件
   * @param {Object} component 查询界面组件实例
   */
  management(component) {
    this._management = component;
    return this;
  }
  /**
   * 设置新增界面组件
   * @param {Object} component 新增界面组件实例
   */
  create(component) {
    this._create = component;
    return this;
  }
  /**
   * 设置编辑界面组件
   * @param {Object} component 编辑界面组件实例
   */
  update(component) {
    this._update = component;
    return this;
  }
  /**
   * 设置详情界面组件
   * @param {Object} component 详情界面组件实例
   */
  detail(component) {
    this._detail = component;
    return this;
  }
  /**
   * 生成
   */
  build() {
    let array = [];
    array.push(
      new Router(`${this._name}-manage`, this._icon, this._title)
        .path(this._path).sort(this._sort)
        .powers(this._powers)
        .component(this._management)
    );

    if (this._create) {
      array.push(
        new Router(`${this._name}-create`, this._icon, `创建${this._title}`)
          .path(`${this._path}/create`)
          .hideInBread()
          .hideInMenu()
          .disableCache()
          .component(this._create)
      );
    }

    if (this._update) {
      array.push(
        new Router(`${this._name}-update`, this._icon, `编辑${this._title}`)
          .path(`${this._path}/update/:id`)
          .hideInBread()
          .hideInMenu()
          .disableCache()
          .component(this._update)
      );
    }

    if (this._detail) {
      array.push(
        new Router(`${this._name}-detail`, this._icon, `${this._title}详情`)
          .path(`${this._path}/detail/:id`)
          .hideInBread()
          .hideInMenu()
          .disableCache()
          .component(this._detail)
      );
    }

    return array;
  }
}
